package com.platform.controller;

import com.alibaba.fastjson.JSON;
import com.platform.service.SysGeneratorService;
import com.platform.utils.PageUtils;
import com.platform.utils.Query;
import com.platform.utils.R;
import com.platform.xss.XssHttpServletRequestWrapper;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.IOUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 代码生成器
 *
 * @author lipengjun
 * @email 939961241@qq.com
 * @date 2017年1月3日 下午6:35:28
 */
@Controller
@RequestMapping("/sys/generator")
public class SysGeneratorController {

  @Autowired
  private SysGeneratorService sysGeneratorService;

  /**
   * 列表
   */
  @ResponseBody
  @RequestMapping("/list")
  @RequiresPermissions("sys:generator:list")
  public R list(@RequestParam Map<String, Object> params) {
    //查询列表数据
    Query query = new Query(params);
    List<Map<String, Object>> list = sysGeneratorService.queryList(query);
    int total = sysGeneratorService.queryTotal(query);

    PageUtils pageUtil = new PageUtils(list, total, query.getLimit(), query.getPage());

    return R.ok().put("page", pageUtil);
  }

  /**
   * 生成代码
   */
  @RequestMapping("/code")
  @RequiresPermissions("sys:generator:code")
  public void code(HttpServletRequest request, HttpServletResponse response) throws IOException {
    String[] tableNames = new String[]{};
    //获取表名，不进行xss过滤
    HttpServletRequest orgRequest = XssHttpServletRequestWrapper.getOrgRequest(request);
    String tables = orgRequest.getParameter("tables");
    tableNames = JSON.parseArray(tables).toArray(tableNames);

    byte[] data = sysGeneratorService.generatorCode(tableNames);

    response.reset();
    response.setHeader("Content-Disposition", "attachment; filename=\"AutoCode.zip\"");
    response.addHeader("Content-Length", "" + data.length);
    response.setContentType("application/octet-stream; charset=UTF-8");

    IOUtils.write(data, response.getOutputStream());
  }
}
